<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" charset="UTF-8">
   
$(function() {
	$('#moduls_user_datagrid').datagrid({
		url : '${pageContext.request.contextPath}/userAction!query.action',
		fit : true,
		border : false,
		pagination : true,
		rownumbers : true,
		checkOnSelect : false,
		selectOnCheck : false,
		idField : 'userId',
		nowrap : false,
		columns : [ [ {
			title : '编号',
			field : 'userId',
			width : 150,
			checkbox : true
		}, {
			title : '工 号',
			field : 'username',
			width : 80
		}, {
			title : '用户姓名',
			field : 'userRealName',
			width : 80
		}, {
			title : '所属部门',
			field : 'groupId',
			width : 150,
			formatter : function(value, row, index) {
				if (value == 0)
					return '';
				return row.groupName;
			}
		}, {
			title : '所属角色',
			field : 'roleId',
			width : 150,
			formatter : function(value, row, index) {
				if (value == 0)
					return '';
				return row.roleName;
			}
		}, {
			field : 'action',
			title : '动作',
			width : 100,
			formatter : function(value, row, index) {
				if (row.id == '0') {
					return '系统角色';
				} else {
					return formatString('<img onclick="moduls_user_editFun(\'{0}\');" src="{1}"/>&nbsp;&nbsp;&nbsp;<img onclick="moduls_user_deleteFun(\'{2}\');" src="{3}"/>&nbsp;&nbsp;&nbsp;<img onclick="moduls_user_modifyUserPassFun(\'{4}\');" src="{5}"/>', row.userId, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.userId, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png', row.userId, 'jslib/jquery-easyui-1.3.1/themes/icons/lock_edit.png');
				}
			}
		} ] ],
		toolbar : '#moduls_user_datagrid_toolbar'
	});
});

function moduls_user_editFun(id) {
	$('#moduls_user_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var index = $('#moduls_user_datagrid').datagrid('getRowIndex', id);
	$('#moduls_user_datagrid').datagrid('checkRow', index);
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/editUser.jsp',
		width : 500,
		height : 200,
		modal : true,
		title : '编辑用户',
		buttons : [ {
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#moduls_fun_editUser_addForm').form('submit', {
					url : '${pageContext.request.contextPath}/userAction!modify.action',
					success : function(result) {
						try {
							var r = $.parseJSON(result);
							if (r.success) {
								$('#moduls_user_datagrid').datagrid('load');
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						} catch (e) {
							$.messager.alert('提示', result);
						}
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			var rows = $('#moduls_user_datagrid').datagrid('getChecked');
			$('#moduls_fun_editUser_addForm').form('load', rows[0]);
			$('#moduls_fun_editUser_addForm input[name=groupId]').combobox('setText',rows[0].groupName);
		}
	});
}
function moduls_user_addFun() {
	$('#moduls_user_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/addUser.jsp',
		width : 500,
		height : 200,
		modal : true,
		title : '添加用户',
		buttons : [ {
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#moduls_fun_addUser_addForm').form('submit', {
					url : '${pageContext.request.contextPath}/userAction!add.action',
					success : function(result) {
						var r = $.parseJSON(result);
						if (r.success) {
							$('#moduls_user_datagrid').datagrid('load');
							d.dialog('destroy');
						}
						$.messager.show({
							title : '提示',
							msg : r.msg
						});
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		}
	});
}
function moduls_user_removeFun() {
	var rows = $('#moduls_user_datagrid').datagrid('getChecked');
	var ids = [];
	if (rows.length > 0) {
		$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
			if (r) {
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].userId);
				}
				$.ajax({
					url : '${pageContext.request.contextPath}/userAction!delete.action',
					data : {
						ids : ids.join(',')
					},
					dataType : 'json',
					success : function(result) {
						if (result.success) {
							$('#moduls_user_datagrid').datagrid('load');
							$('#moduls_user_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
						}
						$.messager.show({
							title : '提示',
							msg : result.msg
						});
					}
				});
			}
		});
	} else {
		$.messager.show({
			title : '提示',
			msg : '请勾选要删除的记录！'
		});
	}
}
function moduls_user_deleteFun(id) {
	$('#moduls_user_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var index = $('#moduls_user_datagrid').datagrid('getRowIndex', id);
	$('#moduls_user_datagrid').datagrid('checkRow', index);
	moduls_user_removeFun();
}

function moduls_user_modifyUserPassFun(id) {
	$('#moduls_user_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var index = $('#moduls_user_datagrid').datagrid('getRowIndex', id);
	$('#moduls_user_datagrid').datagrid('checkRow', index);
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/modifyUserPass.jsp',
		width : 300,
		height : 150,
		modal : true,
		title : '修改密码',
		buttons : [ {
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#moduls_fun_modifyUserPass_modifyForm').form('submit', {
					url : '${pageContext.request.contextPath}/userAction!modifyPass.action',
					success : function(result) {
						try {
							var r = $.parseJSON(result);
							if (r.success) {
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						} catch (e) {
							$.messager.alert('提示', result);
						}
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			var rows = $('#moduls_user_datagrid').datagrid('getChecked');
			$('#moduls_fun_modifyUserPass_modifyForm input[name=userId]').val(rows[0].userId);
		}
	});
}
</script>
<table id="moduls_user_datagrid"></table>

<div id="moduls_user_datagrid_toolbar">
	<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="moduls_user_addFun();">增加</a>
	<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="moduls_user_removeFun();">批量删除</a>
	<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="$('#moduls_user_datagrid').datagrid('reload');">刷新</a>
	<div id="moduls_user_datagrid_toolbar_group" style="width:190px; height: 200px; overflow: auto;">  
	    <div data-options="name:'0',iconCls:'icon-group'">请选择部门</div> 
	    <div class="menu-sep"></div>   
	</div>  
	<input id="moduls_user_datagrid_toolbar_user"></input>  
</div>

<script type="text/javascript" charset="UTF-8">
$(function() {
	
	$('#moduls_user_datagrid_toolbar_user').searchbox({  
	    searcher:function(username,groupId){  
	        $('#moduls_user_datagrid').datagrid('load', {
	        	groupId : groupId,
	        	q : username
	        });
	    },  
	    menu:'#moduls_user_datagrid_toolbar_group',  
	    prompt:'请输入员工编号'  
	});  
	setTimeout(function() {
		$.ajax({
			type : 'POST',
			url : '${pageContext.request.contextPath}/groupAction!getGroupComboBoxData.action',
			cache : false,
			dataType : 'JSON',
			success : function(group) {
				for(var i = 0; i < group.length; i++) {
					$('#moduls_user_datagrid_toolbar_group').menu('appendItem', {
			    		name: group[i].groupId,
			    		text: group[i].groupName,
			    		iconCls: 'icon-group'
			    	});
				}
			}
		});
	}, 500);
});
</script>